//
//  SLCustomerTestimonialsViewController.m
//  SulitLoanDemo
//
//  Created by 虞嘉伟 on 2025/2/19.
//


#import "SLCustomerTestimonialsViewController.h"
#import <YYKit.h>
#import <Masonry.h>
#import "SLCustomerTestimonialsHeader.h"
#import "SLCustomerTestimonialsTableViewCell.h"

@interface SLCustomerTestimonialsViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UIImageView *decorate;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, copy  ) NSArray *data;
@end

@implementation SLCustomerTestimonialsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self showNavBar:@"Customer Testimonials"];
    
    self.view.backgroundColor = UIColorHex(#F5F5F5);
    [self.view addSubview:self.decorate];
    [self.decorate mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.mas_equalTo(aspectVerticalValue(568)+SAFE_AREA_INSETS.bottom);
    }];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view).inset(20);
        make.top.equalTo(self.navBar.mas_bottom);
        make.bottom.equalTo(self.view);
    }];
    [self initData];
}
- (void)initData {
    _data = @[
        @{
            @"avatar": @"icon_avatar",
            @"name": @"Maria, Manila",
            @"reply": @"Thanks to this app, I got my loan in just 1 hour! Highly recommended!",
        },
        @{
            @"avatar": @"icon_avatar",
            @"name": @"Juan, Cebu",
            @"reply": @"Easy to use and fast approval. Perfect for emergencies",
        },
        @{
            @"avatar": @"icon_avatar",
            @"name": @"Juan, Cebu",
            @"reply": @"I needed money urgently for my daughter's school fees. This app approved my loan in just 30 minutes! Thank you so much!",
        },
        @{
            @"avatar": @"icon_avatar",
            @"name": @"Liza, Manila",
            @"reply": @"The interest rates are very reasonable compared to other lenders. No hidden fees, and everything is clear from the start.",
        },
        @{
            @"avatar": @"icon_avatar",
            @"name": @"Anna, Davao",
            @"reply": @"The application process is so simple! I didn’t need to submit a lot of documents, and the steps were easy to follow.",
        },
    ];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _data.count;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    SLCustomerTestimonialsHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"SLCustomerTestimonialsHeader"];
    return header;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SLCustomerTestimonialsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SLCustomerTestimonialsTableViewCell" forIndexPath:indexPath];
    NSDictionary *dict = _data[indexPath.row];
    cell.item.titleLabel.text = dict[@"reply"];
    cell.item.avatar.image = [UIImage imageNamed:dict[@"avatar"]];
    cell.item.nameLabel.text = dict[@"name"];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (UIImageView *)decorate {
    if (!_decorate) {
        _decorate = [UIImageView new];
        _decorate.backgroundColor = UIColor.blackColor;
        _decorate.contentMode = UIViewContentModeScaleAspectFit;
        _decorate.layer.cornerRadius = 20;
    }
    return _decorate;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = UIColor.clearColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:SLCustomerTestimonialsHeader.class forHeaderFooterViewReuseIdentifier:@"SLCustomerTestimonialsHeader"];
        [_tableView registerClass:SLCustomerTestimonialsTableViewCell.class forCellReuseIdentifier:@"SLCustomerTestimonialsTableViewCell"];
    }
    return _tableView;
}

@end
